package com.smit.livevideo.sample.activity;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.smit.adapter.DVBDevice;
import com.smit.adapter.IDVBDeviceCallback;
import com.smit.livevideo.R;
import com.smit.livevideo.sample.MyApplication;
import com.smit.livevideo.sample.dao.ChannelDAO;
import com.smit.livevideo.sample.fragment.FragmentUtil;
import com.smit.livevideo.sample.util.SDKAPIUtil;
import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/7.
 * 融合电视第一次启动时候启动LauncherActivity
 */
public class LauncherActivity extends BaseActivity implements IDVBDeviceCallback, Handler.Callback
{
    private static final String TAG = LauncherActivity.class.getSimpleName();
    private static final int MSG_AD_SHOW_FINISH = 1001;
    private boolean isFirstTimeInitOk = true;
    private int adShowDuration = 1000;
    private Handler handler;
    private ImageView adImageView = null;
    private AnimationDrawable logoAnimation;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        LogUtil.trace(TAG, "onCreate~~~~~");
        SDKAPIUtil.getDVBDevice().addCallback(this);
        setContentView(R.layout.activity_launcher);
        adImageView = (ImageView) findViewById(R.id.iv_launcher_loading_logo);
        Drawable logoDrawable = findViewById(R.id.iv_launcher_loading_logo).getBackground();

        if (logoDrawable instanceof AnimationDrawable)
        {
            LogUtil.debug(TAG, "logo is animation");
            logoAnimation = (AnimationDrawable) logoDrawable;
        }
        handler = new Handler(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        LogUtil.debug(TAG, "onWindowFocusChanged(" + hasFocus + ")");
        super.onWindowFocusChanged(hasFocus);
        if (logoAnimation == null)
        {
            return;
        }
        if (hasFocus)
        {
            logoAnimation.start();
        }
        else
        {
            logoAnimation.stop();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case MSG_AD_SHOW_FINISH:
                adImageView.setVisibility(View.INVISIBLE);
                if (ChannelDAO.getInstance().queryAllChannelList().size() == 0)
                { //没有频道直接进入搜台
                    SDKAPIUtil.getServiceControl().stopDVBService();
                    FragmentUtil.showFragment(this, FragmentUtil.getSearchProgressFragment(),
                            R.id.fl_launcher_new_search_progress_container, true);
                }
                else
                { //进入直播
                    FragmentUtil.popAllBackStack(this.getFragmentManager());
                    MyApplication.getInstance().startLiveVideoActivity();
                    LauncherActivity.this.finish();
                }
                break;
            case DVBDevice.DVB_DEVICE_CONNECTED:
                deviceConnected();
                break;
            case DVBDevice.DVB_DEVICE_DISCONNECTED:
                deviceDisconnect();
                break;
            case DVBDevice.DVB_DEVICE_ERROR:
                break;
            case DVBDevice.DVB_DEVICE_HAS_RESET:
                break;
            case DVBDevice.DVB_DEVICE_INIT_OK:
                deviceInitOK();
                break;
            case DVBDevice.DVB_DEVICE_INIT_FAIL:
                deviceInitFail();
                break;
            case DVBDevice.DVB_DEVICE_IS_RESETTING:
                LogUtil.trace(TAG, "DVB_DEVICE_IS_RESETTING~~~~~");
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    protected void onResume()
    {
        LogUtil.trace(TAG, "onResume~~~~~");
        getWindow().getDecorView().setSystemUiVisibility(View.GONE);
        SDKAPIUtil.getSystemControl().resume();
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        LogUtil.trace(TAG, "onPause~~~~~");
        super.onPause();
    }

    @Override
    protected void onStop()
    {
        LogUtil.trace(TAG, "onStop~~~~~");
        isFirstTimeInitOk = true;
        super.onStop();
    }

    @Override
    protected void onDestroy()
    {
        LogUtil.debug(TAG, "onDestroy~~~~~" + this);
        handler.removeCallbacksAndMessages(null);
        SDKAPIUtil.getDVBDevice().removeCallback(this);
        super.onDestroy();
    }

    /**
     * home键监听调用函数
     */
    void onHomePressed()
    {
        LogUtil.debug(TAG, "onHomePressed~~~");
        exitLiveApp(true);
    }

    /**
     * 设备初始化OK后回调函数
     */
    public void deviceInitOK()
    {
        LogUtil.trace(TAG, "deviceInitOK~~~~");
        super.deviceInitOK();
        /* 这里因为dongle复位会重新进来 */
        if (isFirstTimeInitOk)
        {
            isFirstTimeInitOk = false;
            handler.sendEmptyMessageDelayed(MSG_AD_SHOW_FINISH, adShowDuration);
        }
    }

    /**
     * 设备连接后回调函数
     */
    public void deviceConnected()
    {
        LogUtil.trace(TAG, "deviceConnected~~~~");
        super.deviceConnected();
    }

    /**
     * 设备断开后回调函数
     */
    public void deviceDisconnect()
    {
        LogUtil.debug(TAG, "deviceDisconnect~~~");
        isFirstTimeInitOk = true;
        FragmentUtil.popAllBackStack(getFragmentManager());
        super.deviceDisconnect();
    }

    /**
     * 设备初始化失败后回调函数
     */
    public void deviceInitFail()
    {
        super.deviceInitFail();
        LogUtil.trace(TAG, "deviceInitFail~~~~");
    }

    @Override
    void onNetworkOk()
    {
    }

    @Override
    boolean isMediaPlayerError() {
        return false;
    }

    @Override
    void onNetworkFail()
    {
    }

    @Override
    public void handleDVBDeviceCallbackMsg(int i, Object o)
    {
        LogUtil.debug(TAG, "handleDVBDeviceCallbackMsg:" + i);
        Message handlerMsg = handler.obtainMessage(i, o);
        handlerMsg.sendToTarget();
    }
}
